package com.cql.controller;

import com.cql.pojo.Person;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/cql")
@RefreshScope
public class SayHelloController {

    @Value("${redis.ip}")
    String ip;

    //openfeign风格的请求方式，传统的get和post请求方式
    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    public String sayHello(@RequestParam("name") String name, HttpServletRequest servletRequest) {
            System.out.println(ip);
        return "Hello.01111 ".concat(name).concat(ip);
    }

    @RequestMapping("/rest/{name}")
    public String restHello(@PathVariable("name") String name) {
        return "rest.01 ".concat(name);
    }


    @RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
    public String sayHello2(@PathVariable("name") String name, HttpServletRequest servletRequest) {
        System.out.println(name);
        return "Hello.from service1".concat(name).concat("!");


        // return new Person("a", 1);
    }

    @RequestMapping(value = "/getPerson", method = RequestMethod.POST, consumes = "application/json")
    String getPerson(@RequestBody Person param) {
        System.out.println(param.getName());
        return param.getName().toString();
    }

    @RequestMapping(value = "/fileUpload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String fileUpload(@RequestPart(value = "file") MultipartFile file){
        System.out.println(file.getOriginalFilename());
        return file.getOriginalFilename();
    };
}
